const fs = require('fs');
const cheerio = require('cheerio')
const request = require('request')
const iconv = require('iconv-lite');

let host = "http://sooopu.com"
let reqPromise = function (url) {
    return new Promise((resolve, reject) => {
        request(url, { encoding: null }, (error, response, body) => {

            let buf = iconv.decode(body, 'gb2312')
            let html = buf.toString('utf-8')
            resolve(html)
            reject('请求出错')
        })
    })
}


// 先获取这个列表
async function getList(){
    let html=await reqPromise('http://sooopu.com')
    let $=cheerio.load(html)
    let urls=[]
    $('#comment > table > tbody > tr:nth-child(2) > td > div > ul > li > a').each(function(){
        urls.push(this.attribs.href)
    })
    control(urls)
}
getList()

// 控制并发数量
function control(data, limit = 1) {
    let list = []//存储返回的数据
    let count = 0
    return new Promise((resolve, reject) => {
        // 设定请求函数
        let start = function () {
            let task = data.shift()
            // 如果有任务
            if (task) {
                count++
                console.log(count)
                let url=encodeURI(host + task)
                reqPromise(url).then(res => {
                    console.log(url)
                    count--
                    let $ = cheerio.load(res)
                    // 照片需要处理下
                    let src
                    $('#box > div.content  img').each(function () {
                        src = this.attribs.src
                        if(src.indexOf('@')!=-1){
                            src=src.split('@')[0]
                        }
                        if(src.indexOf('http')==-1){
                            src=host+src
                        }
                    })
                    let photoUrl = encodeURI( src)
                    let filename = src.slice(src.lastIndexOf('/') + 1)
                    let dirName = 'comment'
                    if (!fs.existsSync('./' + dirName)) {
                        fs.mkdirSync('./' + dirName)
                    }
                    let ws = fs.createWriteStream('./' + dirName + '/' + filename)
                    request(photoUrl).pipe(ws)
                    console.log(photoUrl)
                    ws.on('finish',function(){
                        console.log('文件写入成功')
                    })
                    let detail = {
                        comment: $('#plnew > ul > li > p:nth-child(1) > a').text().slice(2),
                        photo: dirName + '/' + filename
                    }
                    // console.log(detail)
                    list.push(detail)
                    data.length && start()
                    // 如果执行数量为0 ,全部执行完成
                    if (count == 0) {
                        // console.log(list,'list')
                        resolve(list)
                        fs.appendFileSync('./comment.json', JSON.stringify(list, null, 4))

                    }
                })
            }
        }
        // 只开limit个同时并发的请求
        for (let n = 0; n < limit; n++) {
            start()
        }
    })
}
// control(data).then(res => {
// })
